package com.goldgov.sensitive.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.goldgov.authorityInfo.service.AuthorityInfoInit;
import com.goldgov.kcloud.core.json.JsonErrorObject;
import com.goldgov.kcloud.core.json.JsonObject;
import com.goldgov.kcloud.core.json.JsonQueryObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.login.web.model.LoginAuthUser;
import com.goldgov.sensitive.service.Sensitive;
import com.goldgov.sensitive.service.SensitiveQuery;
import com.goldgov.sensitive.service.SensitiveService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/workbench/sensitive")
@Api("敏感词服务接口")
public class SensitiveController {
	
	@Autowired
	private SensitiveService service;
	
	@PostMapping
	@ApiOperation(value = "新增敏感词", notes ="")
	public JsonObject<Object> addSensitive(Sensitive obj,HttpServletRequest request){
//		SwbAuthUser u=LoginAuthUser.getUserByAdmin(request);
//		obj.set(u.getEntityID());
//		obj.setUserName(u.getUserName());
//		obj.setAuditState(SensitiveQuery.AUDIT_STATE_ONE);
//		if(obj.getCreateDate()==null||obj.getCreateDate().equals("")) {
//			obj.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
//		}
		service.addSensitive(obj);
		return JsonSuccessObject.SUCCESS;
	}
	
	@DeleteMapping
	@ApiOperation(value = "批量删除敏感词", notes ="删除敏感词")
	public JsonObject<Object> deleteSensitive(@ApiParam(value = "sensitiveIDs", required = true) @RequestParam("ids") String[] ids){
		service.deleteSensitive(ids);
		return JsonSuccessObject.SUCCESS;
	}
	
	@GetMapping("/{id}")
	@ApiOperation(value = "根据主键查询敏感词", notes ="敏感词详情")
	public JsonSuccessObject<Sensitive> getSensitive(@ApiParam(value="sensitiveID",required=false) @PathVariable("id") String id){
		Sensitive obj = service.getSensitive(id);
		JsonSuccessObject<Sensitive> returnResult = new JsonSuccessObject<>(obj);
		return returnResult;
	}
	
	@GetMapping("/")
	@ApiOperation(value = "查询多条敏感词", notes ="敏感词列表")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchCreateDateBegin", value = "敏感词创建时间", paramType = "query"),
		 @ApiImplicitParam(name = "searchCreateDateEnd", value = "敏感词创建时间", paramType = "query"),
		 @ApiImplicitParam(name = "searchTriggerValue", value = "敏感词", paramType = "query")})
	public JsonQueryObject<Sensitive> listSensitive(@ApiIgnore SensitiveQuery query,HttpServletRequest request){
		if(query.getSearchTreePath()==null || "".equals(query.getSearchTreePath())) {
			query.setSearchScopeCode(LoginAuthUser.getScopeCodeByFunctionID(AuthorityInfoInit.zbq,request));
		}
		List<Sensitive> list = service.listSensitive(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@GetMapping("/judgeSensitive")
	@ApiOperation(value = "判断是否含有敏感词", notes ="判断")
	public JsonObject<Object> judgeSensitive(Sensitive sensitive,HttpServletRequest request){
		String flag=service.judgeSensitive(sensitive.getTriggerValue());
		//true代表含有关键词，返回状态为2000，否则为5000
		//flag不为空时代表含有关键词，返回状态为2000，否则为5000
		if(flag.equals(""))
			return JsonErrorObject.ERROR;
		else
			return JsonSuccessObject.SUCCESS;
	}
}